package com.dzl.calculator;

import com.alibaba.fastjson.JSONObject;
import com.dzl.base.controller.BaseController;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 计算器
 * @author liuguanjun
 * @date 2021-10-12 09:16:29
 */
@RestController
@RequestMapping("/welfareCalculator")
public class WelfareCalculatorController extends BaseController {


    /**
     * 计算器计算税金成本毛利和毛利率
     * @author liuguanjun
     * @date 2021-10-12 09:21:29
     * @param param 公司,采购发票等参数
     * @return 税金成本 毛利 毛利率
     **/
    @PostMapping(value = "")
    public JsonResult<Map> updateOrderSignDes(@RequestBody JSONObject param) {
        Integer company = param.getInteger("company");//公司: 1定制链 2所见礼业
        Integer invoiceType = param.getInteger("invoiceType");//采购发票: 1专票 2普票
        BigDecimal salesVolume = param.getBigDecimal("salesVolume");//销售金额
        Integer taxRate = param.getInteger("taxRate");//采购税率
        BigDecimal purchaseAmount = param.getBigDecimal("purchaseAmount");//采购金额
        if(EmptyUtils.isEmpty(company)){
            return JsonResult.paramErrWithMsg("请选择公司");
        }
        if(company!= 1 && company!=2){
            return JsonResult.paramErrWithMsg("company:公司传参有误");
        }
        if(EmptyUtils.isEmpty(invoiceType)){
            return JsonResult.paramErrWithMsg("请选择采购发票");
        }
        if(invoiceType!= 1 && invoiceType!=2){
            return JsonResult.paramErrWithMsg("采购发票传参有误");
        }
        if(EmptyUtils.isEmpty(salesVolume)){
            return JsonResult.paramErrWithMsg("请填写销售金额");
        }
        if(salesVolume.compareTo(BigDecimal.ZERO)<1){
            return JsonResult.paramErrWithMsg("销售金额必须大于0");
        }
        if(EmptyUtils.isEmpty(taxRate)){
            return JsonResult.paramErrWithMsg("请选择采购税率");
        }
        if(taxRate<=0 || taxRate>=100){
            return JsonResult.paramErrWithMsg("taxRate:采购税率传参有误");
        }
        if(EmptyUtils.isEmpty(purchaseAmount)){
            return JsonResult.paramErrWithMsg("请填写采购成本");
        }
        if(purchaseAmount.compareTo(BigDecimal.ZERO)<1){
            return JsonResult.paramErrWithMsg("采购成本必须大于0");
        }
        //返回结果
        Map resultMap = Maps.newHashMapWithExpectedSize(3);
        //税金成本
        BigDecimal taxCost = BigDecimal.ZERO;
        //毛利
        BigDecimal grossProfit = BigDecimal.ZERO;
        //毛利率
        BigDecimal grossProfitRate = BigDecimal.ZERO;
        if(company == 1){//如果是定制链公司
            taxCost = salesVolume.divide(new BigDecimal(1.13),4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(0.13));
            if(invoiceType==1){//如果是专票
                BigDecimal taxRate_ = new BigDecimal(taxRate).divide(new BigDecimal(100),4,BigDecimal.ROUND_HALF_UP);
                taxCost = taxCost.subtract(purchaseAmount.divide((taxRate_.add(new BigDecimal(1))),4,BigDecimal.ROUND_HALF_UP).multiply(taxRate_));
            }
            taxCost = taxCost.multiply(new BigDecimal(1+0.12)).setScale(2, BigDecimal.ROUND_HALF_UP);
        }else{
            taxCost = salesVolume.divide(new BigDecimal(1.01),4,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(0.01));
            taxCost = taxCost.multiply(new BigDecimal(1+0.12)).setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        //计算毛利
        grossProfit = salesVolume.subtract(purchaseAmount).subtract(taxCost).setScale(2, BigDecimal.ROUND_HALF_UP);
        //计算毛利率
        grossProfitRate = grossProfit.divide(salesVolume,4,BigDecimal.ROUND_HALF_UP).setScale(2, BigDecimal.ROUND_HALF_UP);
        resultMap.put("taxCost",taxCost);
        resultMap.put("grossProfit",grossProfit);
        resultMap.put("grossProfitRate",grossProfitRate);
        return JsonResult.success(resultMap);
    }
}
